package com.cloud.school.app.controller;

import com.cloud.school.app.config.security.SecurityUserUtils;
import com.cloud.school.app.model.ChatRoomModel;
import com.cloud.school.app.service.ChatRoomService;
import com.cloud.school.app.service.impl.CacheService;
import com.cloud.school.app.util.Pager;
import com.cloud.school.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ChatRoom
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 1:05
 * @Version 1.0
 */
@Api(tags = "聊天室管理控制器")
@RestController
@RequestMapping("/chatRoom")
public class ChatRoomController {

    public static final String CHAT_ROOM_USER_ID_KEY = "chatRoomUserIds.chatRoomId";

    @Autowired
    private CacheService cacheService;
    @Autowired
    private ChatRoomService chatRoomService;

    @ApiOperation("所有聊天室")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseUtil<List<ChatRoomModel>> all() {
        return ResponseUtil.ok(chatRoomService.selectAll());
    }

    @ApiOperation("根据名称模糊查询聊天室")
    @RequestMapping(value = "/nameLike", method = RequestMethod.GET)
    public ResponseUtil<List<ChatRoomModel>> nameLike(@ApiParam("聊天室名称") @RequestParam(value = "name") String name) {
        return ResponseUtil.ok(chatRoomService.selectByNameLike(name));
    }

    @ApiOperation("分页查询所有聊天室")
    @RequestMapping(value = "/queryByPage", method = RequestMethod.GET)
    public ResponseUtil<Pager<ChatRoomModel>> all(@ApiParam("专业id") @RequestParam(defaultValue = "1") int pNo) {
        return ResponseUtil.ok(chatRoomService.selectAll(pNo));
    }

    @ApiOperation("创建聊天室")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseUtil<Boolean> passInvitation(@ApiParam("聊天室名称") @RequestParam(value = "name") String name, @ApiParam("聊天室简介") @RequestParam(value = "summary") String summary) {
        ChatRoomModel chatRoomModel = new ChatRoomModel();
        chatRoomModel.setName(name);
        chatRoomModel.setSummary(summary);
        chatRoomModel.setCreateUid(SecurityUserUtils.getCurrentUserId());
        return ResponseUtil.ok(chatRoomService.save(chatRoomModel));
    }

    @ApiOperation("加入聊天室")
    @RequestMapping(value = "/enter", method = RequestMethod.POST)
    public ResponseUtil<Boolean> enter(@ApiParam("聊天室id") @RequestParam Long chatRoomId) {
        cacheService.setPut(CHAT_ROOM_USER_ID_KEY + chatRoomId, SecurityUserUtils.getCurrentUserId().toString());
        return ResponseUtil.ok(true);
    }

    @ApiOperation("退出聊天室")
    @RequestMapping(value = "/exit", method = RequestMethod.POST)
    public ResponseUtil<Boolean> exit(@ApiParam("聊天室id") @RequestParam Long chatRoomId) {
        cacheService.setRemove(CHAT_ROOM_USER_ID_KEY + chatRoomId, SecurityUserUtils.getCurrentUserId().toString());
        return ResponseUtil.ok(true);
    }
}
